addEvent(window, "load", tabs_init);

function tabs_init() {
    if (!document.getElementsByTagName) return;
    var divs = document.getElementsByTagName("div");
	var i;
	for (i = 0; i < divs.length; i++)
	{
		thisDiv = divs[i];
		if ((' '+thisDiv.className+' ').indexOf("tab_container") != -1)
		{
			tabs_makeTabs(thisDiv);
		}
	}
}

function tabs_makeTabs(div)
{
	var tab_pages = getElementsByClass(div, "tab_page");
	var tr = document.createElement("tr");
	var i;

	for (i = 0; i < tab_pages.length; i++)
	{
		var td = document.createElement("td");
		var text = document.createTextNode(tab_pages[i].id);
		td.appendChild(text);
		td.tabsystem = div;
		td.tabindex = i;
		td.onclick = function () { showTab(this); }
		tr.appendChild(td);
	}
	var tbody = document.createElement("tbody");
	tbody.appendChild(tr);
	var table = document.createElement("table");
	table.appendChild(tbody);
	table.className = 'tab_header';
	div.insertBefore(table, tab_pages[0]);

	showTab(table.rows[0].cells[0]);
}

function showTab(info)
{
	var tabsystem = info.tabsystem;
	var index = info.tabindex;
	var i;

	tabheader = getElementsByClass(tabsystem, "tab_header")[0];
	cells = tabheader.rows.item(0).cells;
	for (i = 0; i < cells.length; i++)
	{
		if (i == index)
		{
			cells[i].style.background = 'lightgrey url("/img/border.png") no-repeat top left';
//			cells[i].style.border = 'inset';
//			cells[i].style.borderWidth = '2px';
		}
		else
		{
			cells[i].style.background = '#999 url("/img/border.png") no-repeat top left';
//			cells[i].style.border = 'outset';
//			cells[i].style.borderWidth = '2px';
		}
	}

	tabpages = getElementsByClass(tabsystem, "tab_page");
	for (i = 0; i < tabpages.length; ++i)
	{
		if (i == index) tabpages[i].style.display = 'block';
		else tabpages[i].style.display = 'none';
	}
}

